<template>
	<view>
		<view class="cu-card case" :class="isCard?'no-card':''">
			<view class="cu-item shadow">
				<view class="image imageac">
					<image :src="qjhc.value"
					 mode="widthFix"></image>
				</view>
			</view>
		</view>
		<view v-if="qjhc.tzgg" class="cu-bar bg-white solid-bottom margin-top">
			<view class="action">
				<text class="cuIcon-title text-blue"></text>{{qjhc.tzgg}}
			</view>
		</view>
		<uni-section v-if="qjhc.ankz" :title="tsxx" type="line">
			<view class="example" style="padding: 10px;">
				<!-- 基础表单校验 -->
				<uni-forms ref="valiForm" :rules="rules" :model="valiFormData" labelWidth="80px">
					<uni-forms-item label="姓名" required name="xm">
						<uni-easyinput type="text" v-model.trim="valiFormData.xm" placeholder="姓名" />
					</uni-forms-item>
					<uni-forms-item label="身份证" required name="zjhm">
						<uni-easyinput type="idcard" v-model.trim="valiFormData.zjhm" placeholder="身份证" />
					</uni-forms-item>
					<uni-forms-item label="手机" required name="sjhm">
						<uni-easyinput :disabled="true" type="text" v-model.trim="valiFormData.sjhm" placeholder="手机号" />
					</uni-forms-item>
				</uni-forms>
				<button type="default" style="margin: 10px 10px;" open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber1">获取微信绑定的手机号</button>
				<button style="margin-bottom: 50px;" :loading="loading" :disabled="loading" type="primary" @click="submit('valiForm')">提交</button>
			</view>
		</uni-section>
	</view>
</template>
<script>
	var _this;
	var vk = uni.vk;
	import uniForms from "@/pages_modules/components/uni-forms/uni-forms";
	import uniFormsItem from "@/pages_modules/components/uni-forms-item/uni-forms-item";
	export default {
		components:{uniForms,uniFormsItem},
		data() {
			// 页面数据变量
			return {
				xsinfo:{},
				options:{
					bjid:'',
					scene:''
				},
				userid:'',
				wxuserinfo:{},
				bjinfo:{},
				getfalg:false,
				logininfo:false,
				qjhc:{
					ankz:false
				},
				syimg:'https://mp-7c5f2fa7-1d2c-41f1-b3dd-1f45e490fd36.cdn.bspapp.com/cloudstorage/939374e2-cdad-4e6f-b088-9546464e94ea.jpg',
				tsxx:'',
				loading: false,
				// 校验规则
				rules: {
					xm: {
						rules: [{
							required: true,
							errorMessage: '姓名错误'
						},{
							minLength: 1,
							maxLength: 18,
							errorMessage: '请输入正确的姓名',
						}]
					},
					zjhm: {
						rules: [{
							required: true,
							errorMessage: '证件错误'
						},{
							minLength: 18,
							maxLength: 18,
							errorMessage: '请输入正确的证件',
						}]
					},
					sjhm: {
						rules: [{
							required: true,
							errorMessage: '请获取号码'
						}]
					}
				},
				valiFormData:{xm:"",zjhm:"",sjhm:""},
				isCard: false,
				// init请求返回的数据
				data:{
					
				},
				// 表单请求数据
				form1:{
					
				},
				scrollTop:0,
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		async onLoad(options = {}) {
			vk = uni.vk;
			_this = this;
			this.options.scene = decodeURIComponent(options.scene);
			// 二维码参数 班级id
			//await this.getvkGlobalData();
			await this.getxxcjcxglobal();
			await this.getbjinfo();
			//获取用户信息
			_this.wxuserinfo = vk.getVuex('$user.userInfo');
			
			if(vk.myfn.isEmptyObject(_this.wxuserinfo)){
				uni.showToast({
					title: '微信未登录，请先登录，3秒后跳转到登录页面',
					icon:'none'
				})
				setTimeout(function(){
					vk.redirectTo('/pages/index/index3');
				},3000)
			}
			
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){
			
		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {
			
		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {
			
		},
		// 监听 - 点击右上角转发时
		onShareAppMessage(options) {
			
		},
		// 函数
		methods: {
			asyncFunction() {  
				return new Promise((resolve, reject) => {
					vk.setVuex('$user.userInfo.xsinfo',_this.xsinfo);
					setTimeout(() => {
						resolve("代码运行完成");  
					}, 666);  
				});  
			},
			getPhoneNumber1(e){
				let that = this;
				// 微信新增了code参数，可以直接传code，不再需要传 encryptedData 和 iv
				let { code } = e.detail;
				if (!code) {
					return false;
				}
				vk.userCenter.getPhoneNumber({
					data:{
						code,
						encryptedKey: that.encryptedKey
					},
					success: (data) => {
						_this.valiFormData.sjhm = data.phone
					}
				});
			},
			addxsxxgl(res){
				return vk.callFunction({
				  url: 'client/gsxy/jwc/kh/jwcapi.addxsxxgl',
				  title:'请求中...',
				  data:{
					xm:res.xm,
					sfzh:res.zjhm,
					sjhm:res.sjhm,
					bjinfo:_this.bjinfo,
					wxinfo:_this.wxuserinfo
				  }
				}).then((data) => {

					if(data.code == 0){
						_this.logininfo = true;
					}else{
						_this.logininfo = false;
					}
				}).catch((err) => {
				  
				});
			},
			getbjinfo(){
				return vk.callFunction({
				  url: 'client/gsxy/jwc/pub/jwcapi.getbjinfo',
				  title:'请求中...',
				  data:{
					  bjid:_this.options.scene
				  }
				}).then((data) => {
					
					_this.bjinfo = data.info
					//console.log(_this.bjinfo)
					if(_this.bjinfo &&_this.bjinfo._id){}else{
						uni.showToast({
							title: '班级已被删除,请联系辅导员，3秒后自动退出',
							icon:'none'
						})
						setTimeout(function(){
							vk.redirectTo('/pages/index/index');
						},3000)
					}

				}).catch((err) => {
				  
				});
			},
			getxxcjcxglobal(){
				return vk.callFunction({
				  url: 'client/gsxy/globalDataCacheKye/pub/vk-global-data.findById',
				  title:'请求中...',
				  data:{
				    _id:'65a72944e0ec190274c0b5e4'
				  }
				}).then((data) => {
					_this.qjhc = data.info;
				}).catch((err) => {});
			},
			getvkGlobalData(){
				return vk.callFunction({
				  url: 'client/gsxy/jwc/pub/jwcapi.getvkGlobalDataByid',
				  title:'请求中...',
				  data:{
					  byid:'65430d017ad52d2a064685c8'
				  }
				}).then((data) => {
					//console.log(data.info)
					_this.qjhc = data.info;
					//_this.syimg = data.info.value
				 
				}).catch((err) => {
				  
				});
			},
			submit(ref) {
				_this.loading = true
					this.$refs[ref].validate().then(async res => {

						_this.xsinfo.xm=res.xm
						_this.xsinfo.zjhm=res.zjhm
						_this.xsinfo.sjhm=res.sjhm
						_this.xsinfo.bjid=_this.bjinfo._id
						_this.xsinfo.bjid=_this.bjinfo.bjmc

						await _this.asyncFunction();

						await _this.addxsxxgl(res);

						if(_this.logininfo){
							_this.loading = false;
							_this.valiFormData.zjhm = '';
							uni.showToast({
								title: '登记成功，2秒后返回主页',
								icon:'none'
							})
							setTimeout(function(){
								vk.redirectTo('/pages/index/index');
							},2000)
						}else{
							_this.loading = false;
							_this.valiFormData.zjhm = '';
						}
						_this.loading = false;
					}).catch(err => {
						
						// _this.valiFormData.zjhm = '';
						// _this.valiFormData.xm = '';
						uni.showToast({
							title: err[0].errorMessage,
							icon:'none'
						})
						_this.loading = false;
					})
			}
		},
		// 监听器
		watch:{
			
		},
		// 计算属性
		computed:{
			
		}
	}
</script>
<style lang="scss" scoped>
	// .imageac{
	// 	width: 360px;
	// 	height: 166px;
	// }
	
</style>